<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="inp.name"
      ></el-input>
      <el-button @click="del">清除</el-button>
      <el-button type="primary" @click="submit">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="List" row-key="id">
      <el-table-column type="index" label="任务编号" width="80" prop="id">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"></el-table-column>
      <el-table-column prop="des" label="任务描述"></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="newlys(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delbtn(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        :current-page="params._page"
        @current-change="page"
      >
      </el-pagination>
      <el-button size="small" plain @click="newly">+ 新增</el-button>
    </div>
    <el-dialog
      :title="form.id ? '编辑' : '新增'"
      :visible.sync="dialogFormVisible"
      @close="closeDialog"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="form.des" style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchdata,
  getdata,
  getquerydata,
  setData,
  deldata,
  addname,
} from "@/api/user";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      List: [],
      inp: {
        name: "",
      },
      form: {
        name: "",
        des: "",
        id: "",
      },
      id: uuidv4(),
      rules: {
        name: [{ required: true, message: "必填", trigger: "blur" }],
        des: [{ required: true, message: "必填", trigger: "blur" }],
      },
      params: {
        _page: 1, // 当前第几页
        _limit: 5, // 每页条数
      },
      total: 0,
      dialogFormVisible: false,
    };
  },
  mounted() {
    this.getaddlist();
  },
  methods: {
    async getaddlist() {
      const res = await fetchdata(this.params);
      this.List = res.data;
      console.log(res);
      this.total = +res.headers[`x-total-count`];
    },
    // 分页
    page(page) {
      this.params._page = page;
      this.getaddlist();
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.form = {};
    },
    newly() {
      this.dialogFormVisible = true;
    },
    async newlys(id) {
      this.dialogFormVisible = true;
      const res = await setData(id);
      this.form = res.data;
    },
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        if (this.form.id) {
          await getquerydata(this.form);
        } else {
          // this.form.id = uuidv4();
          await getdata({ ...this.form, id: this.id });
        }
        this.id = uuidv4();
        this.$message.success(`${this.form.id ? "编辑" : "新增"}成功`);
        this.closeDialog();
        this.getaddlist();
      });
    },
    async delbtn(id) {
      await deldata(id);
      this.$message.success("删除成功");
      this.getaddlist();
    },
    // 搜索
    async submit() {
      const { data } = await addname(this.inp);
      this.List = data;
      this.total = 1;
    },
    // 清空
    del() {
      this.inp.name = "";
      this.getaddlist();
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
